package listmapdemo;

import java.time.LocalDateTime;
import java.util.*;


public class OrderProcessing {
    // 使用TreeMap存储订单（按时间排序）
    private Map<LocalDateTime, Order> orders = new TreeMap<>();

    // 使用PriorityQueue处理优先级订单（会员优先）
    private PriorityQueue<Order> priorityOrders = new PriorityQueue<>(
            Comparator.comparing(Order::isMemberPriority).thenComparing(Order::getTimestamp)
    );

    // 生成订单号（UUID）
    public String generateOrderId() {
        return UUID.randomUUID().toString();
    }

    // 创建订单
    public Order createOrder(shoppingcart cart) {
        String orderId = generateOrderId();
        LocalDateTime timestamp = LocalDateTime.now();
        Order order = new Order(orderId, timestamp, new HashMap<>(cart.getCart()));
        orders.put(timestamp, order);

        // 如果是会员订单，加入优先队列
        if (order.isMemberPriority()) {
            priorityOrders.add(order);
        }

        return order;
    }

    // 处理下一个优先级订单
    public Order processNextPriorityOrder() {
        return priorityOrders.poll();
    }

    // 获取所有订单按时间排序
    public Collection<Order> getAllOrders() {
        return orders.values();
    }
}

// 订单类
class Order {
    private String orderId;
    private LocalDateTime timestamp;
    private Map<product, Integer> items;
    private boolean memberPriority;

    public Order(String orderId, LocalDateTime timestamp, Map<product, Integer> items) {
        this.orderId = orderId;
        this.timestamp = timestamp;
        this.items = items;
        // 默认非会员订单
        this.memberPriority = false;
    }

    // 为会员订单设置优先级
    public Order(String orderId, LocalDateTime timestamp, Map<product, Integer> items, boolean memberPriority) {
        this.orderId = orderId;
        this.timestamp = timestamp;
        this.items = items;
        this.memberPriority = memberPriority;
    }

    public String getOrderId() {
        return orderId;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public Map<product, Integer> getItems() {
        return Collections.unmodifiableMap(items);
    }

    public boolean isMemberPriority() {
        return memberPriority;
    }
}


